package com.dpiinc.TechPage.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.model.jbean.GoodsInfo;
import com.bumptech.glide.Glide;

import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class GoodsListRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final Context context;
    private List<GoodsInfo> goodList;
    private View mHeaderView;

    public static final int TYPE_HEADER = 0;

    public static final int TYPE_NOMAL = 1;

    public View getHeaderView() {
        return mHeaderView;
    }

    public GoodsListRecyclerViewAdapter(Context context,List<GoodsInfo> goodList) {
        this.context = context;
        this.goodList = goodList;
    }

    public void setHeaderView(View mHeaderView) {
        this.mHeaderView = mHeaderView;
        notifyItemInserted(0);//插入下标0位置
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return getItemViewType(position) == TYPE_HEADER
                            ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if(lp != null
                && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(holder.getLayoutPosition() == 0);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderView != null && viewType == TYPE_HEADER){
            return new GridViewHolder(mHeaderView);
        }
        View baseView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gridview_goods_list, parent, false);
        GridViewHolder gridViewHolder = new GridViewHolder(baseView);
        return gridViewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if(position == 0){
            return;
        }
        GridViewHolder gridViewHolder= (GridViewHolder) holder;
        final int pos = getRealPosition(gridViewHolder);
        gridViewHolder.tvName.setText(goodList.get(pos).getName());
        gridViewHolder.tvPrice.setText("¥ "+goodList.get(pos).getPrice());
        Glide.with(context).load(goodList.get(pos).getImg()).into(gridViewHolder.iv);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        if (position%2==1) {
            lp.setMargins((int)context.getResources().getDimension(R.dimen.dp8), (int)context.getResources().getDimension(R.dimen.dp8), 0, 0);
            gridViewHolder.rightView.setVisibility(View.VISIBLE);
        }else {
            lp.setMargins(0, (int)context.getResources().getDimension(R.dimen.dp8), (int)context.getResources().getDimension(R.dimen.dp8), 0);
            gridViewHolder.rightView.setVisibility(View.GONE);
        }
        gridViewHolder.goodsLayout.setLayoutParams(lp);
        gridViewHolder.goodsLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setData(Uri.parse(goodList.get(pos).getLink()));
                intent.setAction(Intent.ACTION_VIEW);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemViewType(int position) {
        if(mHeaderView == null){
            return TYPE_NOMAL;
        }
        if(position == 0){
            return TYPE_HEADER;
        }
        return TYPE_NOMAL;
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? goodList.size() : goodList.size()+1;
    }

    private int getRealPosition(GridViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    public class GridViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout goodsLayout;
        private ImageView iv;
        private TextView tvName, tvPrice;
        private View rightView;

        public GridViewHolder(View itemView) {
            super(itemView);
            if(itemView == mHeaderView){ return; }
            goodsLayout = itemView.findViewById(R.id.goods_layout);
            iv=  itemView.findViewById(R.id.imageview);
            tvName= itemView.findViewById(R.id.tv_name);
            tvPrice= itemView.findViewById(R.id.tv_price);
            rightView=itemView.findViewById(R.id.view_right);
        }
    }
}
